<template>
  <el-dialog
    :title="groudId ? '编辑分组' : '创建分组'"
    :class="[$store.state.appPrefix + '-c-add-course-dialog', 'g-dialog']"
    width="448px"
    :show-close="!isLoading"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    :loading="isLoading">
    <el-form
      class="is-comfortable"
      label-position="left"
      v-loading="isLoading"
      label-suffix="："
      label-width="140px"
      ref="basicForm"
      :rules="rules"
      :model="basicForm">
      <el-form-item label="分组名称" prop="groupName" class="is-required">
        <el-input
          v-if="!groudId"
          size="small"
          type="textarea"
          :rows="4"
          placeholder="可同时输入多个分组，以顿号隔开，如：销售一部、营销中心"
          resize="none"
          v-model="basicForm.groupName">
        </el-input>
        <el-input
          v-else
          v-model="basicForm.groupName"
          size="small"
          clearable
          show-word-limit>
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :disabled="isLoading" @click="dialogVisible = false">取 消</el-button>
      <el-button :disabled="isLoading" type="primary" @click="$debounceSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* global _ */
import checkForm from 'mixins/checkform'
import { addCourseStuGroup, updateCourseStuGroup } from '../models'

export default {
  mixins: [
    checkForm
  ],
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    groudId: {
      type: String,
      default: ''
    },
    courseId: {
      type: String,
      default: ''
    },
    groupName: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      isLoading: false,
      basicForm: {
        groupName: ''
      },
      rules: {
        groupName: [
          { required: true, message: '请输入分组名称', trigger: 'blur' }
        ]
      },
      debounceSave: null
    }
  },
  watch: {
    dialogVisible(val) {
      this.$emit('update:dialogVisible', val)
      if (!this.groudId) {
        this.basicForm.groupName = ''
      }
    },
    groupName() {
      this.basicForm.groupName = this.groupName
    }
  },
  methods: {
    $debounceSave() {
      if (!this.debounceSave) {
        this.debounceSave = _.debounce(this.$handleSave, 500)
      }
      this.debounceSave()
    },
    initName(name) {
      const tempNames = name.split('、')
      return tempNames.join(',')
    },
    async $handleSave() {
      this.isLoading = true
      if (!this.$checkForm('basicForm')) {
        this.isLoading = false
        return
      }
      const params = {
        ...this.basicForm,
        courseId: this.courseId,
        groupName: this.initName(this.basicForm.groupName)
      }
      let func = addCourseStuGroup
      if (this.groudId) {
        params.id = this.groudId
        func = updateCourseStuGroup
      }
      try {
        await func(params)
        this.$message.success('保存成功')
        this.$emit('saveSucc', () => {
          this.isLoading = false
        })
      } catch (err) {
        this.isLoading = false
        this.$message.error(err.message || err.message || '保存信息出错')
      }
    }
  }
}
</script>
